﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GtsServer.Common;
using GtsServer.DAL;
using Pub.Class;

namespace GtsServer.Charge.Api
{
    public partial class ZfbCallBack : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //获取notify_data数据，需要添加notify_data=
                //不需要解密，直接是明文xml格式
                string notify_data = Request.Form["notify_data"].ToString();

                //获取签名
                string sign = Request.Form["sign"].ToString();

                //验证签名
                //bool vailSign = Function.Verify(notify_data, sign, Config.Alipaypublick, Config.Input_charset_UTF8);
                //if (!vailSign)
                //{
                //    Response.Write("fail");
                //    return;
                //}

                //获取交易状态
                string trade_status = Function.GetStrForXmlDoc(notify_data, "notify/trade_status");

                if (!trade_status.Equals("TRADE_FINISHED"))
                {

                    Response.Write("fail");
                }
                else
                {
                    ///////////////////////////////处理数据/////////////////////////////////
                    // 用户这里可以写自己的商业逻辑
                    // 例如：修改数据库订单状态
                    // 以下数据仅仅进行演示如何调取
                    // 参数对照请详细查阅开发文档
                    // 里面有详细说明
                    //string partner = Function.GetStrForXmlDoc(notify_data, "notify/partner");
                    //string discount = Function.GetStrForXmlDoc(notify_data, "notify/discount");
                    //string payment_type = Function.GetStrForXmlDoc(notify_data, "notify/payment_type");
                    //string subject = Function.GetStrForXmlDoc(notify_data, "notify/subject");
                    //string trade_no = Function.GetStrForXmlDoc(notify_data, "notify/trade_no");
                    //string buyer_email = Function.GetStrForXmlDoc(notify_data, "notify/buyer_email");
                    //string gmt_create = Function.GetStrForXmlDoc(notify_data, "notify/gmt_create");
                    //string quantity = Function.GetStrForXmlDoc(notify_data, "notify/quantity");
                    string out_trade_no = Function.GetStrForXmlDoc(notify_data, "notify/out_trade_no");
                    //string seller_id = Function.GetStrForXmlDoc(notify_data, "notify/seller_id");
                    //string is_total_fee_adjust = Function.GetStrForXmlDoc(notify_data, "notify/trade_status");
                    //string total_fee = Function.GetStrForXmlDoc(notify_data, "notify/total_fee");
                    //string gmt_payment = Function.GetStrForXmlDoc(notify_data, "notify/gmt_payment");
                    //string seller_email = Function.GetStrForXmlDoc(notify_data, "notify/seller_email");
                    //string gmt_close = Function.GetStrForXmlDoc(notify_data, "notify/gmt_close");
                    //string price = Function.GetStrForXmlDoc(notify_data, "notify/price");
                    //string buyer_id = Function.GetStrForXmlDoc(notify_data, "notify/buyer_id");
                    //string use_coupon = Function.GetStrForXmlDoc(notify_data, "notify/use_coupon");
                    ////////////////////////////////////////////////////////////////////////////

                    OrderDAL orderDAL = new OrderDAL();
                    string json = string.Empty;

                    string orderID = out_trade_no;// GetTypeName("out_trade_no").ToStr();

                    // GetTypeName("trade_status") == "TRADE_FINISHED" ? Convert.ToInt32(OrderStutas.成功) : Convert.ToInt32(OrderStutas.不成功);
                    int status = Convert.ToInt32(OrderStutas.成功);

                    if (!string.IsNullOrEmpty(orderID))
                        orderDAL.UpdateOrderStutas(orderID, status);

                    //成功必须在页面上输出success，支付宝才不会再发送通知
                    Response.Write("success");
                }
            }
        }
    }
}